package org.example.jdk8;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

public class test5 {
    public static void main(String[] args) {
        Stu stu1 = new Stu("zhangsan", 10);
        Stu stu2 = new Stu("lisi", 20);
        Stu stu3 = new Stu("wangwu", 15);
        Stu stu4 = new Stu("zhaoliu", 18);

        List<Stu> list = Arrays.asList(stu1, stu2, stu3, stu4);

        /*Collections.sort(list,Stu::comparatorByAge);
        list.forEach(item -> System.out.println(item.getAge()));
        Collections.sort(list,Stu::compareByName);
        list.forEach(item -> System.out.println(item.getName()));*/

        /*list.sort(Stu::comparatorByAge);
        list.forEach(item -> System.out.println(item.getAge()));
        list.sort(Stu::compareByName);
        list.forEach(item -> System.out.println(item.getName()));*/

        /*StuComparator stuComparator = new StuComparator();
        list.sort(stuComparator::comparatorByAge);
        list.forEach(item -> System.out.println(item.getAge()));
        list.sort(stuComparator::compareByName);
        list.forEach(item -> System.out.println(item.getName()));*/

        /*list.sort(Stu::comparatorByAge1);
        *//*
        * 类名：：方法名的调用由lambda表达式第一个参数作为调用者，后续的参数都作为方法的参数
        * *//*
        list.forEach(item -> System.out.println(item.getAge()));
        list.sort(Stu::compareByName1);
        list.forEach(item -> System.out.println(item.getName()));*/



        /*List<String> cities = Arrays.asList("qingdao", "shanghai", "beijing");
        Collections.sort(cities,(a,b)->a.compareToIgnoreCase(b));
        //等价于
        Collections.sort(cities,String::compareToIgnoreCase);

        Collections.sort(list,(s1,s2)->s1.comparatorByAge1(s2));
        //等价于
        Collections.sort(list,Stu::comparatorByAge1);*/


        System.out.println(new Test().getString(String::new));
        System.out.println(new Test().getString1("hello",String::new));

    }
}
class Stu{
    private String name;
    private Integer age;
    public Stu(String name,Integer age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    //比较器 设计上不太合理
    public static int comparatorByAge(Stu stu1,Stu stu2){
        return stu1.getAge()-stu2.getAge();
    }
    public static int compareByName(Stu stu1,Stu stu2){
        return stu1.getName().compareToIgnoreCase(stu2.getName());
    }

    //比较器 设计上合理，传入与当前类比较
    public int comparatorByAge1(Stu stu){
        return this.getAge()-stu.getAge();
    }
    public int compareByName1(Stu stu){
        return this.getName().compareToIgnoreCase(stu.getName());
    }
}

class StuComparator{
    public int comparatorByAge(Stu stu1,Stu stu2){
        return stu1.getAge()-stu2.getAge();
    }
    public int compareByName(Stu stu1,Stu stu2){
        return stu1.getName().compareToIgnoreCase(stu2.getName());
    }
}


class Test{
    public String getString(Supplier<String> supplier){
        return supplier.get()+"test";
    }
    public String getString1(String str, Function<String,String> function){
        return function.apply(str);
    }
}